﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.IO;
using System.Drawing;

namespace ImageUploader.domain
{
    public class ExportUsers
    {
        private string fileName;
        private string[] fields;
        private LDAPConnection ldapConn;

        public ExportUsers(string fileName, LDAPConnection ldapConn, string[] fields)
        {
            this.ldapConn = ldapConn;
            this.fields = fields;
            this.fileName = fileName;
        }

        public void ExportUsersToCsv()
        {            
            DomainController dc = new DomainController(ldapConn);
            String text = "";
            //text += "givenName" + ";" + "cn" + ";" + "sn" + /* ";" + "mail" + ";" + "displayName" + ";" + "telephoneNumber" + ";" + "postalCode" + ";" + "physicalDeliveryOfficeName" + ";" + "postOfficeBox" + ";" + "wWWHomepage" + ";" + "initials" + ";" + "description" + "st" + ";" + "streetAddress" + ";" + "l" + ";" + "co" +  /*"jpegPhoto" +*/ "\n";
            //System.Text.Encoding enc = System.Text.Encoding.UTF8;
            //System.Text.Encoding enc = System.Text.Encoding.BigEndianUnicode;
            System.Text.Encoding enc = System.Text.Encoding.Default;
            for (int i = 0; i < fields.Length; i++)
            {
                text += fields[i] + ((i != fields.Length - 1) ? ";" : "\n");
            }
            int counter = 0;
            //List<User> users=dc.GetUsers(fields.ToList());
            List<User> users = dc.GetNonSystemCriticalUsers(fields.ToList());
            

            foreach (User u in users)
            {
                using (DirectoryEntry deUser = u.DirectoryEntry)
                {
                    //   text += u.FirstName + ";" + u.CN + ";" + u.LastName +/* ";" + u.Mail + ";" + u.DisplayName + ";" + u.Telnr + u.PostalCode + ";" + u.Office + ";" + u.PostOfficeBox + ";" + u.HomePage + ";" + u.Initials + ";" + u.Description + /*";" + u.Province + ";" + u.Street + u.Place + ";" + u.Country + System.Text.Encoding.ASCII.GetString(u.JpgPhoto) +*/ "\n";
                    foreach (String s in fields)
                    {
                        if ((deUser.Properties[s].Value != null && (deUser.Properties[s].Value is String
                            || deUser.Properties[s].Value is int
                            || deUser.Properties[s].Value is byte[])))
                        {

                            if (deUser.Properties[s].Value is byte[])
                            {
                                //&& deUser.Properties[s].PropertyName.ToUpper().Equals("JPEGPHOTO") )
                                //   text += BitConverter.ToString((byte[])deUser.Properties[s].Value);
                                // .Replace("-", "");
                                foreach (byte b in (byte[])deUser.Properties[s].Value)
                                {
                                    text += @"\" + b.ToString("x2");
                                }
                            }
                            else
                                text += deUser.Properties[s].Value.ToString();
                        }
                        text += ";";
                        
                    }
                    text = text.Substring(0, text.Length - 1) + "\n";
                            //(deUser.Properties[s].Value is byte[]):deUser.Properties[s].Value)?:
                            // deUser.Properties[s].Value.ToString() : "") + ";";
                }
                //}//enc.GetString(input)
                
                counter++;
                if (ProgressChanged != null)
                    ProgressChanged(this, new ProgressEventArgs((int)((((float)counter / users.Count)) * 100)));
            }
            System.IO.File.WriteAllText(fileName, text);
        }
        public event ImageUploader.domain.Delegates.ProgressHandler ProgressChanged;
    }
}

